const network = require('../../utils/network')

Page({
  data: {
    phoneText: '',
    totpText: '',
    realnameText: '',
    passwordText: '',
    totpStatus: 0, // 0 未发送，1 已发送
    totpBtnText: '',
    region: '',
    regionSel: [],
    school: {},
    schoolSel: '',
    schoolRange: [],
    schoolNameRange: [],
    regionIndex: [0, 0, 0],
    provice: [],
    city: [],
    district: [],
    block: false
  },
  onLoad() {
    wx.setNavigationBarTitle({
      title: '用户注册'
    })
  },

  /**
   * @desc 验证
   */
  validate() {
    let phoneReg = /^1\d{10}$/
    let totpReg = /^\d{6}$/
    let passwordReg = /^\w{6,16}$/

    // 电话号码格式检测
    if (!phoneReg.test(this.data.phoneText.trim())) {
      wx.showModal({
        title: '提示',
        content: '请填写正确的手机号',
        showCancel: false
      })
      return false
    }
    // 验证码格式检测
    if (!totpReg.test(this.data.totpText.trim())) {
      wx.showModal({
        title: '提示',
        content: '请填写正确的验证码',
        showCancel: false
      })
      return false
    }
    // 姓名格式检测
    if (!this.data.realnameText.trim()) {
      wx.showModal({
        title: '提示',
        content: '请填写姓名',
        showCancel: false
      })
      return false
    }

    // 密码格式检测
    if (!passwordReg.test(this.data.passwordText.trim())) {
      wx.showModal({
        title: '提示',
        content: '请填写 6 至 16 位密码',
        showCancel: false
      })
      return false
    }

    // 地区检测
    if (!this.data.region) {
      wx.showModal({
        title: '提示',
        content: '请选择地区',
        showCancel: false
      })
      return false
    }

    // 学校检测
    if (!this.data.school.schoolId) {
      wx.showModal({
        title: '提示',
        content: '请选择学校',
        showCancel: false
      })
      return false
    }

    return true
  },

  /**
   * @desc 注册
   */
  register() {
    if (this.validate()) {
      network
        .post('student/userRegistration', {
          tel: this.data.phoneText.trim(),
          password: this.data.passwordText.trim(),
          realname: this.data.realnameText.trim(),
          code: this.data.totpText.trim(),
          schoolId: this.data.school.schoolId
        })
        .then(data => {
          if (data.status == 1) {
            wx.showToast({
              title: '注册成功',
              icon: 'none',
              duration: 2000
            })
            setTimeout(_ => {
              wx.reLaunch({
                url: '/pages/login/login'
              })
            }, 2000)
          } else {
            wx.showToast({
              title: data.results,
              icon: 'none',
              duration: 2000
            })
          }
        })
        .catch(err => {
          console.log(err)
          wx.showToast({
            title: err.data.msg,
            icon: 'none',
            duration: 2000
          })
        })
    }
  },

  /**
   * @desc 获取验证码
   */
  getTotp() {
    let phoneReg = /^1\d{10}$/

    // 电话号码格式检测
    if (!phoneReg.test(this.data.phoneText.trim())) {
      wx.showModal({
        title: '提示',
        content: '请填写正确的手机号',
        showCancel: false
      })
      return false
    }
    if (!this.data.block) {
      this.setData({
        block: true
      })
      network
        .post('user/sendCode', {
          phoneNum: this.data.phoneText.trim()
        })
        .then(data => {
          this.setData({
            block: false
          })
          wx.showToast({
            title: '发送成功',
            icon: 'none',
            duration: 2000
          })
          this.countDown()
        })
        .catch(err => {
          this.setData({
            block: false
          })
          console.log(err)
          wx.showToast({
            title: err.data.msg,
            icon: 'none',
            duration: 2000
          })
        })
    }
  },

  /**
   * @desc 验证码倒计时
   */
  countDown() {
    let count = 90
    this.setData({
      totpStatus: 1,
      totpBtnText: count + '秒'
    })

    let time = setInterval(e => {
      count--
      if (count > 0) {
        this.setData({
          totpBtnText: count + '秒'
        })
      } else {
        this.setData({
          totpBtnText: '',
          totpStatus: 0
        })
        clearInterval(time)
      }
    }, 1000)
  },

  /**
   * @desc 手机号输入
   * @param e
   */
  phoneInput(e) {
    let phoneText = e.detail.value.trim()
    this.setData({
      phoneText
    })
  },

  /**
   * @desc 验证码失焦
   * @param e
   */
  totpBlur(e) {
    let totpText = e.detail.value.trim()
    this.setData({
      totpText
    })
  },

  /**
   * @desc 姓名失焦
   * @param e
   */
  realnameBlur(e) {
    let realnameText = e.detail.value.trim()
    this.setData({
      realnameText
    })
  },

  /**
   * @desc 密码失焦
   * @param e
   */
  passwordBlur(e) {
    let passwordText = e.detail.value.trim()
    this.setData({
      passwordText
    })
  },

  /**
   * @desc 查找学校
   * @param regionRange
   */
  searchSchool(regionRange) {
    let provinceName = regionRange[0].replace('省', ''),
      cityName = regionRange[1].replace('市', ''),
      townName = regionRange[2]

    network
      .post('student/getSchoolInfo', {
        provinceName,
        cityName,
        townName
      })
      .then(data => {
        this.setData({
          schoolRange: data
        })
      })
  },

  /**
   * @desc 学校选择
   * @param {*} e
   */
  schoolChange(e) {
    let schoolIndex = e.detail.value
    this.setData({
      school: this.data.schoolRange[schoolIndex]
    })
  },

  /**
   * @desc 地区选择
   * @param {*} e
   */
  regionChange(e) {
    let regionRange = e.detail.value
    this.setData({
      region: regionRange.join(''),
      school: {}
    })
    this.searchSchool(regionRange)
  }
})
